<?php

	function getPipePos($n, $line)
	{
		$pos = 0;
		for ($i = 0; $i < $n; $i++)
		{
			$pos = strpos($line, "|", $pos + 1);
		}
		return $pos;
	}

	function getRank($line)
	{
		$length = getPipePos(1, $line);
		return substr($line, 0, $length);
	}

	function getField($s, $e, $line)
	{
		$start = getPipePos($s, $line) + 1;
		$length = getPipePos($e, $line) - $start;
		return substr($line, $start, $length);
	}

	$espnfilename = "espnrankings.data";
	$espnfile = file_get_contents("out/" . $espnfilename);
	$espnlines = explode("\n", $espnfile);

	$yahoofilename = "yahoorankings.data";
	$yahoofile = file_get_contents("out/" . $yahoofilename);
	$yahoolines = explode("\n", $yahoofile);


	$NOT_IN_YAHOO = "";
	$NOT_IN_YAHOO_COUNT = 0;
	$i = 0;

	foreach($espnlines as $espnline)
	{
		if ($i > 0)
		{
			$name = getField(1, 2, $espnline);
			$rank = getRank($espnline);
			$found = false;
			foreach($yahoolines as $yahooline)
			{
				$posInEspn = strpos($yahooline, $name);
				if ($posInEspn !== false)
				{
					$found = true;
					break;
				}
			}
			if (!$found)
			{
				$NOT_IN_YAHOO .= $rank . "." . $name . "<br>";
				$NOT_IN_YAHOO_COUNT++;
			}
		}
		$i++;
	}
	echo "<h5>Not In Yahoo! (".$NOT_IN_YAHOO_COUNT.")</h5>";
	echo $NOT_IN_YAHOO;
	// if (file_put_contents("out/aggregatedstats.data", $AGGREGATED_LIST) !== false)
	// {	
	// 	echo "<h3>Successfully aggregated out/$espnfilename to out/$yahoofilename.</h3>" .
	// 		"<h3>Processed data is at out/aggregatedstats.data.</h3>";
	// 	echo "<h6>Not Found in ESPN (".$NOT_IN_ESPN_LIST_COUNT.")</h6>";
	// 	echo $NOT_IN_ESPN_LIST;
	// 	echo "<iframe src='out/aggregatedstats.data' width='100%' height='80%'>";

	
	// }
	// else
	// {
	// 	echo "<h3>Error processing raw data.</h3>";
	// }
?>